package guoanmaker.operator.helper.common.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import redis.clients.jedis.Jedis;

/**
 * 
 * @ClassName: HttpRequestHelper
 * @Description: 获取request请求内容包装类
 * @author xiaoqiang
 * @date 2017年12月13日 上午10:24:45
 */
public class HttpRequestHelper {
	/**
	 * 
	 * @Title: getIp 
	 * @Description: 获取用户ip 
	 * @param request 
	 * @return String 
	 * @throws
	 */
	public static String getIp(HttpServletRequest request) {
		String ip = request.getHeader("X-Real-IP");
		if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
			return ip;
		}
		ip = request.getHeader("X-Forwarded-For");
		if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
			// 多次反向代理后会有多个IP值，第一个为真实IP。
			int index = ip.indexOf(',');
			if (index != -1) {
				return ip.substring(0, index);
			} else {
				return ip;
			}
		} else {
			return request.getRemoteAddr();
		}
	}

	/**
	 * 
	 * @Title: userLogin 
	 * @Description: 判断用户是否登录 
	 * @param request 
	 * @return boolean 
	 * @throws
	 */
	public static boolean userLogin(HttpServletRequest request) {
		Jedis jedis = new Jedis("123.56.48.63", 6379);
		jedis.auth("218007");
		try {
			String currentSessionid = request.getSession().getId();
			String sessionid = request.getHeader("Authorization");
			System.out.println(currentSessionid);
			System.out.println(sessionid);
			if (sessionid != null) {
				currentSessionid = sessionid.substring(7, sessionid.length());
			}
			String value = jedis.get(currentSessionid);
			System.out.println(value);
			return !(value==null || !value.equals("1"));
		} catch (Exception e) {
			return false;
		} finally {
			jedis.disconnect();
		}

	}
}
